﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using EnvDTE;
using EnvDTE80;

namespace SinacorRules
{
    public sealed class Rule_ModelPropertyChanged : BaseAddIn.Rules.BaseRuleAddIn
    {
        #region Campos

        Type _tipoBaseModel;

        #endregion

        #region Constructor

        public Rule_ModelPropertyChanged() :
            base("ModelPropertyChanged", "SinacorRules.SinacorRules", typeof(Rule_ModelPropertyChanged).Assembly)
        {
            _tipoBaseModel = typeof(Sinacor.Infra.UI.Common.BaseModel);
        }

        #endregion

        #region Protected Override Methods

        protected sealed override void Check(CodeProperty propriedade, CodeNamespace namespaceElement, CodeClass2 classe)
        {
            CodeFunction setter;
            EditPoint texto;
            string codigo;

            if (propriedade.Setter != null)
            {
                setter = propriedade.Setter;
                texto = setter.StartPoint.CreateEditPoint();
                codigo = Common.Rotinas.GetTextNoComent(texto.GetText(setter.EndPoint));

                if (Common.Rotinas.CheckClassInheritance(classe, _tipoBaseModel)
                    && !codigo.Contains("OnPropertyChanged"))
                {
                    GeraProblemaAddIn(setter.StartPoint.Parent.Parent.FullName,
                                      setter.StartPoint.Line,
                                      setter.StartPoint.DisplayColumn,
                                      "ModelPropertyChangedResolution",
                                      classe.Name,
                                      propriedade.Name,
                                      namespaceElement.Name);
                }
            }
        }

        #endregion
    }
}
